package com.lgkj.ui;

import android.content.DialogInterface;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;
import com.lgkj.utils.TitleBuilder;
import com.lgkj.view.SelectPicPopupWindow;

import org.devio.takephoto.app.TakePhotoActivity;
import org.devio.takephoto.model.TResult;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/25 0025.
 */

public class UserInfoActivity extends TakePhotoActivity {
    SelectPicPopupWindow menuWindow;
    private String imageId;
    private String sex = "0";

    @BindView(R.id.iv_user)
    ImageView ivUser;
    @BindView(R.id.et_nick)
    EditText etNick;
    @BindView(R.id.et_sex)
    TextView etSex;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_adress)
    EditText etAdress;

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_info;
    }

    @Override
    public void initView() {
        new TitleBuilder(this).setTitleText("个人资料").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        etNick.setText(SharedPreferencesUtils.getParam(UserInfoActivity.this, "nickname", "").toString());
        etPhone.setText(SharedPreferencesUtils.getParam(UserInfoActivity.this, "phone", "").toString());
        String  sex=SharedPreferencesUtils.getParam(UserInfoActivity.this, "sex", "").toString();
        etSex.setText(sex);
        etAdress.setText(SharedPreferencesUtils.getParam(UserInfoActivity.this, "adress", "四川绵阳").toString());
        String loadpath = SharedPreferencesUtils.getParam(UserInfoActivity.this, "loadheadimg", "").toString();
        if (loadpath != null && loadpath.length() > 0) {
            CommonUtils.loadImageRound(UserInfoActivity.this, ivUser, new File(loadpath), 0);
        } else {
            String url = SharedPreferencesUtils.getParam(UserInfoActivity.this, "headimg", "").toString();
            CommonUtils.loadImageRound(UserInfoActivity.this, ivUser, url, 0);
        }
    }

    private void showImage() {
        menuWindow = new SelectPicPopupWindow(UserInfoActivity.this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuWindow.dismiss();
                switch (v.getId()) {
                    case R.id.btn_take_photo:
                        CommonUtils.takeFromCapture(getTakePhoto(), 120, 120);
                        break;
                    case R.id.btn_pick_photo:
                        CommonUtils.takeFromDocuments(getTakePhoto(), 120, 120);
                        break;
                    default:
                        break;
                }
            }
        });
        menuWindow.showAtLocation(UserInfoActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        String url = CommonConstant.getTakeImagePath();
        Log.i(TAG, "showImg: " + url);
        upload(new File(url));
//        CommonUtils.loadImageRound(UserInfoActivity.this,ivUser,"http://md.05948166.com/meidian/Public/Uploads/20180619/20180619_171951_270585_head1529399989.jpg",0);
    }

    private void upload(File file) {
        showDiaLog("图片正在上传");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(UserInfoActivity.this));
        HttpUtils.uploadImage(map, CommonConstant.uploadImg, file, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong("上传成功", 0);
                imageId = jsonObject.optString("id");
                Uri uri = Uri.fromFile(new File(CommonConstant.getTakeImagePath()));
                ivUser.setImageURI(uri);
                CommonUtils.loadImageRound(UserInfoActivity.this, ivUser, new File(CommonConstant.getTakeImagePath()), 0);
            }

            @Override
            public void errorHttp(String msg) {
                toastLong("上传失败", 0);
            }
        });
    }


    @OnClick({R.id.ibtn_save, R.id.iv_user, R.id.ll_sex})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_save:
                userEdit();
                break;
            case R.id.iv_user:
                showImage();
                break;
            case R.id.ll_sex:
                chooiceSex();
                break;


        }
    }

    private void chooiceSex() {
        AlertDialog.Builder builder = new AlertDialog.Builder(UserInfoActivity.this);
        builder.setTitle("请选择性别");
        //    指定下拉列表的显示数据
        final String[] cities = {"保密", "男", "女"};

        //    设置一个下拉的列表选择项
        builder.setItems(cities, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                sex = which + "";
                etSex.setText(cities[which]);
            }
        });
        builder.show();
    }

    private void userEdit() {
        showDiaLog("正在修改");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(UserInfoActivity.this));
        map.put("nickname", etNick.getText().toString());
        map.put("sex", sex);
        if (imageId != null) {
            map.put("headimg", imageId);
        }
        HttpUtils.postHttp(map, CommonConstant.userEdit, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    SharedPreferencesUtils.setParam(UserInfoActivity.this, "sex", etSex.getText().toString());
                    SharedPreferencesUtils.setParam(UserInfoActivity.this, "nickname", etNick.getText().toString());
                    SharedPreferencesUtils.setParam(UserInfoActivity.this, "loadheadimg", CommonConstant.getTakeImagePath());
                    finish();
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }
}
